package com.logistics.inventory.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.container.entity.domian.Container;
import com.logistics.container.service.ContainerService;
import com.logistics.inventory.entity.domain.Inventory;
import com.logistics.inventory.entity.domain.InventoryDetail;
import com.logistics.inventory.entity.model.InventoryDetailPage;
import com.logistics.inventory.entity.model.InventoryDetailRow;
import com.logistics.inventory.entity.model.InventoryDetailSave;
import com.logistics.inventory.entity.model.InventorySave;
import com.logistics.inventory.mapper.InventoryDetailMapper;
import com.logistics.log.entity.domain.Log;
import com.logistics.log.entity.model.LogRow;
import com.logistics.qrcode.service.QrcodeService;
import com.logistics.utils.aspect.annotation.Insert;
import com.logistics.utils.bean.ResultPager;
import com.logistics.utils.constants.Constants;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 拣配明细 服务实现类
 * </p>
 *
 * @author sun.yang
 * @since 2024-06-27
 */
@Service
public class InventoryDetailService extends ServiceImpl<InventoryDetailMapper, InventoryDetail> {


    /**
     * 存在更新不存在新增库存详细
     *
     * @param save
     */
    public void merge(InventoryDetailSave save) {
        InventoryDetail result = this.getOne(new LambdaQueryWrapper<InventoryDetail>().
                eq(InventoryDetail::getContainer, save.getContainer()).eq(InventoryDetail::getItemCode, save.getItemCode()));
        if (StringUtils.isNull(result)) {
            this.save(InventoryDetail.build(save));
        } else {
            // 数量
            BigDecimal qty = new BigDecimal(result.getItemQty().toString()).add(save.getItemQty());
            save.setItemQty(qty);
            this.update(InventoryDetail.build(save), new LambdaQueryWrapper<InventoryDetail>().eq(InventoryDetail::getId, result.getId()));
        }
    }

    /**
     * 查询库存
     *
     * @param inventoryDetailPage
     * @return
     */
    public ResultPager getContainerDetail(InventoryDetailPage inventoryDetailPage) {
        IPage page = new Page(inventoryDetailPage.getCurrent(), inventoryDetailPage.getPageSize());
        QueryWrapper<InventoryDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotNull(inventoryDetailPage.getItemCode()),
                        InventoryDetail::getItemCode, inventoryDetailPage.getItemCode())
                .eq(StringUtils.isNotNull(inventoryDetailPage.getContainer()),
                        InventoryDetail::getContainer, inventoryDetailPage.getContainer())
                .like(StringUtils.isNotNull(inventoryDetailPage.getItemWorkcode()),
                        InventoryDetail::getItemWorkcode, inventoryDetailPage.getItemWorkcode())
                .like(StringUtils.isNotNull(inventoryDetailPage.getItemName()),
                        InventoryDetail::getItemName, inventoryDetailPage.getItemName());
        IPage<InventoryDetail> result = this.page(page, queryWrapper.lambda().orderByDesc(InventoryDetail::getCreateTime));
        return new ResultPager(result.getRecords().stream()
                .map(e -> InventoryDetailRow.build(e)).collect(Collectors.toList()),
                (int) result.getTotal());
    }

    public List<InventoryDetail> getByContainer(List<String> containerList) {
        return this.list(new LambdaQueryWrapper<InventoryDetail>().in(InventoryDetail :: getContainer, containerList));
    }
}
